/**
 * May 12, 2014 10:29:08 AM
 */
package elitech.vietnam.copyweatherfashion.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import elitech.vietnam.copyweatherfashion.R;

/**
 * @author Cong
 *
 */
public class WarningDialogFragment extends DialogFragment implements View.OnClickListener {

	OnButtonClick mClick;
	
	TextView mContent;
	Button mButton;
	int mRequestCode;
	
	String mMessage;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			mClick = (OnButtonClick) getTargetFragment();
			mRequestCode = getTargetRequestCode();
		} catch (ClassCastException e) {
		}
		mMessage = getArguments().getString("MESSAGE");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.warning_dialog, container, false);
		
		mContent = (TextView) view.findViewById(R.id.warningdialog_textMessage);
		mButton = (Button) view.findViewById(R.id.warningdialog_btnYes);
		
		mButton.setOnClickListener(this);
		
		mContent.setText(mMessage);
		return view;
	}
	
	/**
	 * 
	 * @deprecated Should use {@link #show(FragmentManager)} method
	 */
	@Override
	@Deprecated
	public void show(FragmentManager manager, String tag) {
		super.show(manager, tag);
	}
	
	public void show(FragmentManager manager) {
		super.show(manager, getClass().getSimpleName());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirmdialog_btnYes:
			if (mClick != null)
				mClick.doClick(mRequestCode);
			break;
		default:
			break;
		}
		dismiss();
	}
	
	public interface OnButtonClick {
		public void doClick(int requestCode);
	}
	
	public static class Builder {
		int mRequestCode;
		String mMessage;
		Fragment mFragment;
		
		public Builder() {}
		
		public Builder setMessage(String message) {
			mMessage = message;
			return this;
		}
		
		public Builder setTargetFragment(Fragment fragment, int requestCode) {
			mFragment = fragment;
			mRequestCode = requestCode;
			return this;
		}
		
		public WarningDialogFragment build() {
			WarningDialogFragment fragment = new WarningDialogFragment();
			Bundle bundle = new Bundle();
			bundle.putString("MESSAGE", mMessage);
			fragment.setArguments(bundle);
			fragment.setTargetFragment(mFragment, mRequestCode);
			fragment.setStyle(STYLE_NO_TITLE, R.style.Dialog);
			return fragment;
		}
	}
}
